package com.ponhu.ea2.goodsCenter.provider.schedule;

import com.alibaba.fastjson.JSON;
import com.ponhu.ea2.entity.GoodsDataSyncTaskLog;
import com.ponhu.ea2.enums.SyncTaskTypeEnum;
import com.ponhu.ea2.goodsCenter.provider.service.GoodsDataSyncService;
import com.ponhu.ea2.goodsCenter.provider.service.GoodsDataSyncTaskLogService;
import com.ponhu.ea2.goodsCenter.provider.service.InventoryService;
import com.ponhu.ea2.goodsCenter.provider.service.PonHuGoodsDataService;
import com.ponhu.ea2.query.GoodsDataSyncQuery;
import com.ponhu.ea2.vo.GoodsDataAttributeSyncVo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 胖总管商品属性数据同步任务
 *
 * @author zhaokai
 */
@Slf4j
@Component
public class GoodsAttributeDataSyncTask {


    @Resource
    GoodsDataSyncService goodsAttributeDataSync;
    @Resource
    InventoryService inventoryService;
    @Resource
    PonHuGoodsDataService ponHuGoodsDataService;
    @Resource
    GoodsDataSyncTaskLogService goodsDataSyncTaskLogService;

    @XxlJob("goodsAttributeDataSync")
    public ReturnT<String> goodsAttributeDataTask(String param) {
        log.info("胖总管商品属性数据任务执行开始,{}", LocalDateTime.now());
        // 取出最后一次执行任务的最新时间
        Date lastDateEditTime = goodsDataSyncTaskLogService.getLastDataEditTime(SyncTaskTypeEnum.ATTRIBUTE.getCode());
        // 保存任务执行日志
        GoodsDataSyncTaskLog goodsDataSyncTaskLog = new GoodsDataSyncTaskLog();
        goodsDataSyncTaskLog.setStartTime(LocalDateTime.now());
        // 查询参数
        GoodsDataSyncQuery goodsDataSyncQuery = new GoodsDataSyncQuery();
        // 胖总管货品数据
        List<GoodsDataAttributeSyncVo> goodsDataAttributeSyncVos = new ArrayList<>();
        // 查询胖总管数据前先查一下本地是否有数据，如有则表示已经同步过数据，则只取指定时间内，否则取全量数据
        Integer count = inventoryService.count();
        log.info("商品中心属性数据，货品数据已有{}条", count);
        if (count > 0 && null != lastDateEditTime) {
            goodsDataSyncQuery.setDataEditTime(lastDateEditTime);
            goodsDataAttributeSyncVos = ponHuGoodsDataService.selectPhGoodsAttributeData(goodsDataSyncQuery);
        } else {
            // 查询胖总管数据，这里第一次查询出来的数据可能会很大
            goodsDataAttributeSyncVos = ponHuGoodsDataService.selectPhGoodsAttributeData(goodsDataSyncQuery);
        }
        log.info("商品中心属性数据，取出胖总管数据{}条", goodsDataAttributeSyncVos.size());
        if (!CollectionUtils.isEmpty(goodsDataAttributeSyncVos)) {
            // 保存日志的最新更新时间和货品编码值
            goodsDataSyncTaskLog.setDataEditTime(goodsDataAttributeSyncVos.get(0).getDataEditTime());
            goodsDataSyncTaskLog.setCode(goodsDataAttributeSyncVos.get(0).getCode());
            goodsAttributeDataSync.goodsAttributeDataSync(goodsDataAttributeSyncVos);
        }
        log.info("胖总管商品属性数据任务执行结束,{}", LocalDateTime.now());
        // 任务结束时间
        goodsDataSyncTaskLog.setEndTime(LocalDateTime.now());
        Map<String,Object> remarksMap = new HashMap<>();
        remarksMap.put("取出胖总管条数",goodsDataAttributeSyncVos.size());
        goodsDataSyncTaskLog.setRemarks(JSON.toJSONString(remarksMap));
        goodsDataSyncTaskLog.setTaskType(SyncTaskTypeEnum.ATTRIBUTE.getCode());
        goodsDataSyncTaskLogService.save(goodsDataSyncTaskLog);
        return ReturnT.SUCCESS;
    }
}